$(function() {
	activeNav($("#qiyeMessageList"));
	
	$("#detail-cancel").click(function () {
		clearDetail();
	});
	
	$("#detail-close").click(function () {
		clearDetail();
	});
});
/**
 * 详情
 * @param id
 */
function findDetail(id){
	$('#detail-body').show();
	
	$.ajax({
		async : false,
		cache : false,
		dataType : 'JSON',
		type : 'POST',
		url : ctx + '/admin/qiye/message/ajaxMessageById',
		data : {id: id},
		beforeSend : function(){
			$('#detail-body').mask("加载中...");
		},
		success : function(data) {
			$('#detail-body').unmask();
			var message = data[0];
			$(data).each(function(i, message){
				if(message.root){
					if(message.type == 0){
						//文本消息
						$('#detail-content').html(getTextHtml(message));
					}else{
						//图文消息
						$('#detail-content').html(getTextImageHtml(data));
					}
					return;
				}
			});
		},
		error : function() {
			$('#detail-body').unmask();
			$.alert({message: "操作异常，请稍候再试！"});
		}
	});
}
/**
 * 隐藏Dialog
 */
function clearDetail(){
	$('#detail-body').hide();
	$('#detail-content').html('');
}
/**
 * 文本HTML
 * @param message
 * @returns
 */
function getTextHtml(message){
	var html = new Array();
	html.push('<div class="mc_content">');
	
	html.push('<div class="mc_card_to">');
	html.push('<p>系统管理员</p>');
	html.push('<p><span class="label">发送给：</span><span class="text">' + message.target + '</span></p>');
	html.push('<p><span class="label">时间：</span><span class="text">' + new Date(message.publishTime).format("yyyy-MM-dd HH:mm:ss") + '</span></p>');
	html.push('</div>');
	
	html.push('<div class="mc_content_item">');
	html.push('<div class="mc_card_wrap clearfix">');
	
	html.push('<div class="mc_text_wrap mc_text_wrap_green float-left">');
	html.push('<div class="mc_text_middle">');
	html.push('<div class="mc_text_inner">');
	html.push('<div class="mc_detail">' + message.content + '</div>');
	html.push('<span class="mc_vertical"></span>');
	html.push('</div>');
	html.push('</div>');
	html.push('</div>');
	
	html.push('</div>');
	html.push('</div>');
	
	html.push('</div>');
	return html.join('');
}
/**
 * 图文HTML
 * @param messages
 * @returns
 */
function getTextImageHtml(messages){
	var html = new Array();
	html.push('<div class="mc_content">');
	var rootHeadHtml = new Array();
	var rootContentHtml = new Array();
	$(messages).each(function(i, message){
		if(message.root){
			rootHeadHtml.push('<div class="mc_card_to">');
			rootHeadHtml.push('<p>系统管理员</p>');
			rootHeadHtml.push('<p><span class="label">发送给：</span><span class="text">' + message.target + '</span></p>');
			rootHeadHtml.push('<p><span class="label">时间：</span><span class="text">' + new Date(message.publishTime).format("yyyy-MM-dd HH:mm:ss") + '</span></p>');
			rootHeadHtml.push('</div>');
			rootContentHtml.push('<a class="video_image" href="javascript:void(0);" >');
			rootContentHtml.push('<div class="video_image_wrap">');
			rootContentHtml.push('<img src="' + message.picurl + '" /><span class="image_vertical"></span>');
			rootContentHtml.push('</div>');
			rootContentHtml.push('<p class="video_sub_title">' + message.title + '</p>');
			rootContentHtml.push('</a>');
			return;
		}
	});
	var msgHtml = new Array();
	$(messages).each(function(i, message){
		if(!message.root){
			msgHtml.push('<a class="video_artical" href="javascript:void(0);" >');
			msgHtml.push('<img class="video_artical_img" src="' + message.picurl + '" />');
			msgHtml.push('<p class="video_artical_title">' + message.title + '</p>');
			msgHtml.push('<span class="video_vertical"></span>');
			msgHtml.push('</a>');
		}
	});
	html.push(rootHeadHtml.join(''));
	//组装消息内容开始
	html.push('<div class="mc_content_item">');
	html.push('<div class="mc_card_wrap clearfix">');
	html.push('<div class="video_unit">');
	html.push(rootContentHtml.join(''));
	html.push(msgHtml.join(''));
	html.push('</div>');
	html.push('</div>');
	html.push('</div>');
	//组装消息内容结束
	html.push('</div>');
	return html.join('');
}